package controller;

import utils.Codes;
import utils.Messages;
import view.ViewRecommendersPanel;
import viewInterfaces.AbstractViewFactory;
import viewInterfaces.IViewDBConnectionSetup;
import controllerInterfaces.IControllerDBConnectionSetup;
import controllerInterfaces.IControllerMain;
import dao.DAOException;
import dao.DAOFactory;
import dao.DBConnection;
import enums.DBMS;

/**
 * Controller Class manages the database connection setup
 */
public class ControllerDBConnectionSetup implements IControllerDBConnectionSetup {

	/** MVC++ Main Controller
	* @see ControllerMain*/
	private IControllerMain mainController;
	
	/** View of DB connection setup.
	 * @see ViewRecommendersPanel*/	
	private IViewDBConnectionSetup view;
	
	
	/**
	 * Class Constructor */
	public ControllerDBConnectionSetup() {
		this.view = AbstractViewFactory.getAbstractViewFactory().getViewDBConnection();
		this.view.setController(this);	
		this.load();
	}
	
	
	/**
	 * Loads,if it exists, connection data in the view.
	 */
	public void load() {
		
		if(DBConnection.getURI() != null || !DBConnection.getURI().isEmpty())
		{
			this.view.setURI(DBConnection.getURI());
		}
		
		if(DBConnection.getUser() != null || !DBConnection.getUser().isEmpty())
		{
			this.view.setUser(DBConnection.getUser());
		}
		
		if(DBConnection.getPassword() != null || !DBConnection.getPassword().isEmpty())
		{
			this.view.setPassword(DBConnection.getPassword());
		}
		
	}

	
	/**
	 * Show to user the view.
	 */
	public void showView()
	{
		this.view.render();
	}
	
	
	/**
	 * Closes the view and finalizes this controller
	 */
	public void close() {
		this.mainController.stopDBConnectionSetup();
	}

	
	/**
	 * Set the MVC++ Main Class for this controller.</br>
	 * ControllerMain is manipulable through {@link IControllerMain} interface
	 * @param	controllerMain	MVC++ Main Controller
	 * @see	ControllerMain
	 * @see IControllerMain
	 */
	public void SetMainController(IControllerMain controller) {
		this.mainController = controller;
	}

	/**
	 * Realizes the connection to database
	 * @param	URI		identifies to the database
	 * @param	user	database user name
	 * @param	password	user password
	 */
	public void connect(String URI, String user, String password) {
		DBConnection.setURI(URI);
		DBConnection.setUser(user);
		DBConnection.setPassword(password);
		
		try
		{
			if (DAOFactory.getDAOFactory().getTestConnection().isEstablished())
			{
				view.renderConnectionStatus(Codes.successfullDBConnection, Messages.successfullDBConnection, true);
				mainController.setDBConnectedStatus(true);
				this.view.close();
			}
		}
		catch (DAOException e)
		{
			view.renderConnectionStatus(e.getCode(), e.getMessage(), false);
			mainController.setDBConnectedStatus(false);
			//e.printStackTrace();
		}
		
	}
	
	
	
	/**
	 * Get View of this controller</br>
	 * This method is only used for realize unit testing.
	 * @return IViewDBConnectionSetup
	 */
	public IViewDBConnectionSetup GetView()
	{
		return this.view;
	}
	
	
	
	/**
	 * Indicate if application is running on debug mode.
	 * @return value of debug mode.
	 */
	public boolean isDebug(){
		return mainController.isDebug();
	}

}
